<?php defined('SYSPATH') or die('No direct script access.');
require_once(APPPATH. 'classes/model/Model_Mysql.php');

class Model_UserProfile extends Model_Mysql {
	const FEMALE = 0;
	const MALE = 1;
	
	public function getUserProfileData($uid) {
		$query = DB::select()->from('users_profile')->where('uid', "=", $uid);
		$result = $this->execute_single($query);
		if($result == false) {
			$result = array('uid' => $uid, 'f_name' => '', 'l_name' => '', 'sex' => -1,
							'pic_url' => '', 'about_me' => '');
		}
		return $result;
	}
	public function setUserProfileData($uid, $data) {
		$query = DB::query(Database::INSERT, "INSERT INTO users_profile (uid, f_name, l_name, 
				about_me,wall_page_id,dob) VALUES(:uid, :f_name, :l_name, :about_me, :wall_id,:dob);");
		$query->parameters(array(':uid' => $uid, ':f_name' => $data['f_name'],':l_name' => $data['l_name'],
						':about_me' => $data['about_me'], ':wall_id' => $data['wall_id'],':dob'=>$data['dob']));
		$this->execute($query);
	}
	//In data: key should same as db field and should not contain uid
	public function updateUserProfile($uid, $data) {
		$params = array();
		$values = array();
		foreach($data as $key=>$val) {
			$params[":".$key] = $val;
			$values[] = "$key = :$key";
		}
		$sql = "UPDATE users_profile SET ".implode(",", $values)." WHERE uid=$uid";
		$query = DB::query(Database::UPDATE, $sql)->parameters($params);
		$this->execute($query);
	}
	public function getSex($uid) {
		$query = DB::query(Database::SELECT, "SELECT sex FROM users_profile WHERE uid = $uid");
		$result = $this->execute_single($query);
		if($result) return $result['sex'];
		return -1;
	}
}